package com.changpei.core.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.changpei.common.result.Result;
import com.changpei.core.pojo.Favorites;
import com.changpei.core.service.FavoritesService;
import com.changpei.core.utils.SearchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>Title : Favorites</p>
 * <p>Description : favorites的控制层</p>
 *
 * @author weibiao
 * @date 2020年3月7日
 */

@RestController
@RequestMapping("/favorites")
@CrossOrigin
public class FavoritesController {

    @Autowired
    private FavoritesService favoritesService;

    /**
     * <p>Title : add</p>
     * <p>Description : 新增Favorites数据</p>
     *
     * @param favorites
     * @return
     */
    @PostMapping(value = "/add")
    public Result<Long> add(@RequestBody Favorites favorites) {
        // 调用FavoritesService实现添加Favorites
        if (favoritesService.save(favorites)) {
            return Result.ok(favorites.getId());
        }
        return Result.failed();
    }

    /**
     * <p>Title : delete</p>
     * <p>Description : Favorites根据ID数据</p>
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<String> delete(@PathVariable Long id) {
        // 调用FavoritesService实现根据主键删除
        if (favoritesService.removeById(id)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : update</p>
     * <p>Description : Favorites根据ID修改</p>
     *
     * @param favorites
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<String> update(@RequestBody Favorites favorites, @PathVariable Long id) {
        // 设置主键值
        favorites.setId(id);
        // 调用FavoritesService实现修改Favorites
        if (favoritesService.updateById(favorites)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : findPage</p>
     * <p>Description : Favorites分页搜索实现</p>
     *
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<Favorites>> findPage(@PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用FavoritesService实现分页查询Favorites
        Page<Favorites> pageInfo = favoritesService.page(new Page<>(pageNum, pageSize));
        return Result.ok(pageInfo);
    }

    /**
     * <p>Title : findPage</p>
     * <p>Description : Favorites分页条件搜索实现</p>
     *
     * @param favorites
     * @param pageNum   : 当前页
     * @param pageSize  : 每页显示多少条
     * @return
     */
    @PostMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<Favorites>> findPage(@RequestBody(required = false) Favorites favorites, @PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用FavoritesService实现分页条件查询Favorites
        Page<Favorites> pageInfo = favoritesService.page(new Page<>(pageNum, pageSize), SearchUtil.initQuery(favorites));
        return Result.ok(pageInfo);
    }

    /**
     * <p>Title : findList</p>
     * <p>Description : 多条件搜索Favorites数据</p>
     *
     * @param favorites
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Favorites>> findList(@RequestBody(required = false) Favorites favorites) {
        // 调用FavoritesService实现条件查询Favorites
        List<Favorites> list = favoritesService.list(SearchUtil.initQuery(favorites));
        return Result.ok(list);
    }

    /**
     * <p>Title : findById</p>
     * <p>Description : 根据ID查询Favorites数据</p>
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<Favorites> findById(@PathVariable Long id) {
        // 调用FavoritesService实现根据主键查询Favorites
        Favorites favorites = favoritesService.getById(id);
        return Result.ok(favorites);
    }

    /**
     * <p>Title : findOne</p>
     * <p>Description : 多条件搜索Favorites数据，结果返回一条</p>
     *
     * @param favorites
     * @return
     */
    @PostMapping(value = "/findOne")
    public Result<Favorites> findOne(@RequestBody(required = false) Favorites favorites) {
        // 调用FavoritesService实现条件查询Favorites
        Favorites favoritesResult = favoritesService.getOne(SearchUtil.initQuery(favorites));
        return Result.ok(favoritesResult);
    }

    /**
     * <p>Title : findAll</p>
     * <p>Description : 查询Favorites全部数据</p>
     *
     * @return
     */
    @GetMapping(value = "/all")
    public Result<List<Favorites>> findAll() {
        // 调用FavoritesService实现查询所有Favorites
        List<Favorites> list = favoritesService.list();
        return Result.ok(list);
    }
}
